package com.lez.flowpractice2.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.lez.common.LocalEventBus
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch

class SharedFlowViewModel(): ViewModel() {

    private lateinit var job : Job

    fun startRefresh(){
       job =  viewModelScope.launch(Dispatchers.IO) {  //需要指定Dispatchers.IO,在子线程中执行
           while (true){
               LocalEventBus.postEvents(System.currentTimeMillis())
           }
        }
    }
    fun stopRefresh(){
       job.cancel()
    }

}